package com.mobilebsc.datamanager;

import java.util.ArrayList;
import java.util.List;

import com.mobilebsc.datatype.EDataType;
import com.mobilebsc.datatype.KPI;
import com.mobilebsc.exception.NotImplementedException;
import com.mobilebsc.httpclient.callback.IAsyncTaskCompleteListener;
import com.mobilebsc.httpclient.callback.KPIRestClientUsage;

public class KPIDataManager implements IDataManager<KPI>, IAsyncTaskCompleteListener<List<KPI>> {

	private KPIRestClientUsage restClientUsage;
	private List<KPI> resultList;
	private List<KPI> singleResultList;
	private IDataReadyListener dataReadyListener;

	public KPIDataManager(IDataReadyListener dataReadyListener) {
		restClientUsage = new KPIRestClientUsage(this);
		resultList = null;
		singleResultList = null;
		this.dataReadyListener = dataReadyListener;
	}
	
	@Override
	public void getAll() {
		restClientUsage.getAll();
	}

	@Override
	public void getById(int id) {
		restClientUsage.getById(id);
	}
	
	public void getReferencedKPI(int referenceId, EDataType referenceType) {
		restClientUsage.getReferencedKPI(referenceId, referenceType);
	}

	@Override
	public List<KPI> loadAll() {
		if (resultList != null) {
			return resultList;
		} else {
			restClientUsage.getAll();
			return new ArrayList<KPI>();
		}
	}

	@Override
	public List<KPI> loadById(int id) {
		if (resultList != null) {
			for (KPI kpi : resultList) {
				if (id == kpi.getId()) {
					singleResultList = new ArrayList<KPI>();
					singleResultList.add(kpi);
					break;
				}
			}
		} else {
			restClientUsage.getAll();
			singleResultList = new ArrayList<KPI>();
		}

		return singleResultList;
	}

	@Override
	public void onTaskComplete(List<KPI> result) {
		resultList = result;

		// do something with ws response
		dataReadyListener.dataReady();		
	}

	@Override
	public void add(KPI element) {
		throw new NotImplementedException();
		
	}

	@Override
	public void delete(KPI element) {
		throw new NotImplementedException();
		
	}
}